package com.gupao.javavip.rpc.framework.client;

import com.gupao.javavip.rpc.framework.protocol.RPCRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ServiceProxy<T> implements InvocationHandler {

    private Class<T> interfaceType;

    private RPCInvoke rpcInvoke;

    public void setRpcInvoke(RPCInvoke rpcInvoke) {
        this.rpcInvoke = rpcInvoke;
    }

    public ServiceProxy(Class<T> intefaceType, RPCInvoke rpcInvoke) {
        this.interfaceType = interfaceType;
        this.rpcInvoke = rpcInvoke;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RPCRequest request = new RPCRequest();

        String serviceName = method.getDeclaringClass().getName();
        String methodName = method.getName();
        String serviceFlag = serviceName + "#" + methodName;

        request.setService(serviceFlag);
        request.setParams(args);
//        request.setVersion("0");

        System.out.println("RPC调用");
        return rpcInvoke.send(request);
    }
}